package ljl.alg.jianzhioffer.round2;

public class _05_replace_space {
    
    public String replaceSpace(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                sb.append("%20");
            } else {
                sb.append(s.charAt(i));
            }
        }
        return sb.toString();
    }
    
    // 区别不大，开阔思路
    public String replaceSpace1(String s) {
        int spaces = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') spaces++;
        }
        char[] res = new char[s.length() + spaces * 2];
        for (int i = 0, j = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                res[j++] = '%';
                res[j++] = '2';
                res[j++] = '0';
            } else {
                res[j++] = s.charAt(i);
            }
        }
        return new String(res);
    }
}
